import {
  addCart,
  getCartInfo,
  changeCartNum,
  delCartItem,
  changeItemSelected,
} from "@/api/cart";
import { Toast } from "antd-mobile";
export default {
  namespace: "cart",
  state: {
    items: [],
    number: 0,
    price: 0
  },

  reducers: {
    initCart(state: any, action: any){
      const {items, number, price} = action.data;
      state.items = items;
      state.number = number;
      state.price = price;
      return {
        ...state
      }
    }
  },
  // 处理异步
  effects: {
    // 加入购物车  type cart/addCart
    *addCart(action: any, {call, put}) {
      const res = yield call(addCart, action.params);
      if (res.data.code === 0) {
        Toast.show({
          content: '加入购物车成功'
        })
        yield put({
          type: 'initCart',
          data: res.data.data
        })
      }
    },
    // 获取购物车数据
    *getCartInfo (action, {call, put}) {
      const res = yield call(getCartInfo);
      if (res.data.code === 0) {
        yield put({
          type: 'initCart',
          data: res.data.data
        });
      }
    }
  },
};
